package com.example.shuiyin.utils;

import com.example.shuiyin.exception.AppException;
import com.example.shuiyin.exception.ErrorCode;
import com.example.shuiyin.enums.TextStyle;
import com.example.shuiyin.enums.WatermarkPosition;
import lombok.extern.slf4j.Slf4j;
import org.bytedeco.ffmpeg.global.avcodec;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.FFmpegFrameRecorder;
import org.bytedeco.javacv.Frame;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class FFmpegUtils {

    /**
     * 去除视频水印
     * @param inputPath 输入视频路径
     * @param outputPath 输出视频路径
     * @return 处理后的视频路径
     */
    public String removeWatermark(String inputPath, String outputPath) {
        log.info("开始处理视频水印: {} -> {}", inputPath, outputPath);
        
        FFmpegFrameGrabber grabber = null;
        FFmpegFrameRecorder recorder = null;
        
        try {
            // 初始化视频抓取器
            grabber = new FFmpegFrameGrabber(inputPath);
            grabber.start();
            
            // 获取视频信息
            int width = grabber.getImageWidth();
            int height = grabber.getImageHeight();
            double frameRate = grabber.getVideoFrameRate();
            
            // 初始化视频录制器
            recorder = new FFmpegFrameRecorder(outputPath, width, height);
            recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
            recorder.setFormat("mp4");
            recorder.setFrameRate(frameRate);
            recorder.setVideoBitrate(grabber.getVideoBitrate());
            
            if (grabber.getAudioChannels() > 0) {
                recorder.setAudioChannels(grabber.getAudioChannels());
                recorder.setAudioBitrate(grabber.getAudioBitrate());
                recorder.setSampleRate(grabber.getSampleRate());
            }
            
            recorder.start();
            
            // 创建水印坐标
            int watermarkX = (int)(width * 0.7);
            int watermarkY = (int)(height * 0.1);
            int watermarkWidth = (int)(width * 0.3);
            int watermarkHeight = (int)(height * 0.1);
            
            // 处理视频
            Frame frame;
            while ((frame = grabber.grab()) != null) {
                if (frame.image != null) {
                    recorder.record(frame);
                } else if (frame.samples != null) {
                    recorder.record(frame);
                }
            }
            
            log.info("视频水印处理完成");
            return outputPath;
            
        } catch (Exception e) {
            log.error("处理视频水印失败: {}", e.getMessage(), e);
            throw new AppException("处理视频水印失败: " + e.getMessage(), ErrorCode.INTERNAL_SERVER_ERROR);
        } finally {
            try {
                if (grabber != null) {
                    grabber.stop();
                    grabber.release();
                }
                if (recorder != null) {
                    recorder.stop();
                    recorder.release();
                }
            } catch (Exception e) {
                log.error("释放视频处理资源失败", e);
            }
        }
    }
    
    /**
     * 为视频添加水印
     * @param inputPath 输入视频路径
     * @param outputPath 输出视频路径
     * @param watermarkText 水印文本
     * @param position 水印位置
     * @param textStyle 文本样式
     */
    public void processVideoWatermark(String inputPath, String outputPath, String watermarkText, 
                                     WatermarkPosition position, TextStyle textStyle) {
        log.info("开始为视频添加水印: {} -> {}", inputPath, outputPath);
        
        FFmpegFrameGrabber grabber = null;
        FFmpegFrameRecorder recorder = null;
        
        try {
            // 实现视频水印处理逻辑
            // ...
            
        } catch (Exception e) {
            log.error("处理视频水印失败: {}", e.getMessage(), e);
            throw new AppException("处理视频水印失败: " + e.getMessage(), ErrorCode.INTERNAL_SERVER_ERROR);
        } finally {
            // 释放资源
            // ...
        }
    }
} 